﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;

namespace Forum.Filter
{
    public class ApiResultFilterAttribute: ResultFilterAttribute, IResultFilter
    {
        public bool Ignore { get; set; }
        public ApiResultFilterAttribute(bool ignore = false)
        {
            Ignore = ignore;
        }

        public override void OnResultExecuted(ResultExecutedContext context)
        { 
            
        }

        public override void OnResultExecuting(ResultExecutingContext context)
        {
            if (Ignore) return;

            context.Result = new ContentResult
            {
                StatusCode = 200,
                ContentType = "application/json",
                Content = JsonConvert.SerializeObject(context.Result)
            };
        }
    }
}
